<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>04猜数字练习</title>
</head>
<body>
<h3>已生成一个1~100的随机数:</h3>
<input type="text" placeholder="请输入您猜的数字!">
<button onclick="guess()">点我验证</button>
<h3>小提示: <span></span> </h3>

<script>
    //1.生成一个1~100的随机数 [0,1)*100=[0,100)+1=[1,101)
    let n = parseInt(Math.random()*100)+1;
    console.log('打个小抄：'+n);
    //2.定义一个用来统计次数的变量
    var count = 0;
    function guess(){
        let num = document.querySelector('input').value;
        //isNaN()用于判断参数是否为非数字,如果是非数字,结果为true,否则为false
        if(isNaN(num)){
           alert('请输入数字!');
           return;
        }
        let spanE = document.querySelector('span');
        count++;
        if(num>n){
            spanE.innerHTML = '猜大了!';
        }else if(num<n){
            spanE.innerHTML = '猜小了!';
        }else{
            spanE.innerHTML = '恭喜你用了'+count+'次就猜对了!';
        }
    }
</script>
</body>
</html>